# coding：utf-8
from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains

class Commerce:
    """
        菜鸟小王，Tpshop电商购物自动化
        pageobject封装
        powered by Ms.Judy
        2024/4/26
    """
    def __init__(self):

        self.driver = webdriver.Chrome()
        # 最大化
        self.driver.maximize_window()

        # 隐性等待
        self.driver.implicitly_wait(10)

        # 打开网址
        self.driver.get("http://localhost/index.php")


    def login(self,lname,lpw,lvc):
        # 打开登录页面
        self.driver.get("http://localhost/index.php/Home/user/login.html")

        # 输入文字为了登陆成功
        self.driver.find_element_by_xpath('//*[@id="username"]').send_keys(lname)
        self.driver.find_element_by_xpath('//*[@id="password"]').send_keys(lpw)
        self.driver.find_element_by_xpath('//*[@id="verify_code"]').send_keys(lvc)
        self.driver.find_element_by_xpath('//*[@id="loginform"]/div/div[6]/a').click()

        # 校验用户是否正确
        name = self.driver.find_element_by_class_name('mu-m-phone').text
        if name == 'hello':
            print('登录成功')
        else:
            print('账号不匹配，请查看：%s' % name)

    def uplode_img(self,img_path):
        # 上传头像开始
        self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[1]/div/ul[4]/li[2]/a').click()
        self.driver.find_element_by_id('preview').click()

        # 进入frame框架[不熟]
        self.driver.switch_to.frame(self.driver.find_element_by_xpath('//*[@id="layui-layer-iframe1"]'))
        self.driver.find_element_by_xpath('//*[@id="filePicker"]/div[2]/input').send_keys(img_path)


        double = self.driver.find_element_by_xpath("//*[@id='uploader']/div[1]/div[3]/div[3]")
        sleep(7)  # 注意设置等待时间！
        ActionChains(self.driver).double_click(double).perform()
        self.driver.switch_to.default_content()

        # 确认保存
        self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[2]/div/div[2]/form/ul[5]/li[2]/div[3]/input').click()
        # 上传头像结束

    def shopping(self,goods):
        # 加入购物车开始

        self.driver.get("http://localhost/index.php/Home/User/info.html")
        self.driver.find_element_by_xpath("//*[@id='q']").send_keys(goods)
        self.driver.find_element_by_xpath("//*[@id='sourch_form']/a").click()
        self.driver.find_element_by_xpath("/html/body/div[4]/div/div[2]/div[2]/ul/li[2]/div/div[5]/div[2]/a").click()
        self.driver.find_element_by_xpath("//*[@id='join_cart']").click()

        sleep(3)

        # frame跳转页面[成功]
        self.driver.switch_to.frame(self.driver.find_element_by_xpath('//*[@id="layui-layer-iframe1"]'))  # 找到iframe框架
        self.driver.find_element_by_xpath("//*[@id='addCartBox']/div[1]/div/div/a[2]").click()  # 定位文字
        self.driver.switch_to.default_content()

        # 提交订单
        self.driver.find_element_by_xpath("//*[@id='ng-app']/body/div[2]/div[1]/div[2]/div/div/div/div/div[3]/a[2]").click()
        sleep(5)
        self.driver.find_element_by_xpath("//*[@id='cart2_form']/div/div[2]/div[2]/div/a/span").click()

        # 加入购物车结束